$(function(){
	var flag = false;
$('#headImage').click(function() {
	$('#file').click();
});

function createXMLHttpRequest() {
	if(window.ActiveXObject) {
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	} else if(window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}
}

function UpladFile() {
	var f = document.getElementById("file").value;
	if(!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(f)) {
		layer.msg("请选择正确格式的图片");
		return false;
	}
	var token = localStorage.phone;
	var fileObj = document.getElementById("file").files[0];
	var FileController = 'https://newapp.icandrive.cn/api/student_photo.php?token=' + token;
	var form = new FormData();
	form.append("myfile", fileObj);
	createXMLHttpRequest();
	xhr.onreadystatechange = handleStateChange;
	xhr.open("post", FileController, true);
	xhr.send(form);
}

function handleStateChange() {
	if(xhr.readyState == 4) {
		if(xhr.status == 200 || xhr.status == 0) {
			var result = xhr.responseText;
			var json = eval("(" + result + ")");
			$("#headImage").prop('src', json.file);
			flag = true;
		}
		if(xhr.status == 400) {
			layer.msg("照片大小不能超过2M");
			return false;
		}
	}
}
$(".information_btn1").click(function() {
	history.back();
});

$("#username").blur(function() {
	var username = $("#username").val();
	if(username == "") {
		layer.msg("姓名不能为空");
		return false;
	}
});
$("#idcard").blur(function() {
	var idcard = $("#idcard").val();
	if(idcard == "") {
		layer.msg("请输入身份证号码");
		return false;
	} else if(!/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/i.test(idcard)) {
		layer.msg("请填写合法的身份证号");
		return false;
	}
});
$("#work").blur(function() {
	var work = $("#work").val();
	if(work == "") {
		layer.msg("职业不能为空");
		return false;
	}
});
$(".submit_final").click(function() {
	if(!flag) {
		layer.msg("请先上传头像");
		return false;
	}
	var token = localStorage.phone;
	var username = $("#username").val();
	if(username == "") {
		layer.msg("姓名不能为空");
		return false;
	}
	var idcard = $("#idcard").val();
	if(idcard == "") {
		layer.msg("身份证不能为空");
		return false;
	}
	var work = $("#work").val();
	if(work == "") {
		layer.msg("职业不能为空");
		return false;
	}
	if(!$('#drive_argument').prop('checked')) {
		layer.msg("请先阅读i驾车报名协议");
		return false;
	}

	$.ajax({
		url: "https://newapp.icandrive.cn/api/saveUserInfo.php",
		type: 'post',
		dataType: 'json',
		data: {
			token: token,
			user_name: username,
			id_number: idcard,
			work: work
		},
		success: function(data) {
			/*console.log(data);*/
			/*return false;*/
			if(data.status == 402) {
				layer.msg("认证系统服务关闭");
			}
			if(data.status == 403) {
				layer.msg("身份信息不匹配");
			}
			if(data.status == 200) {
				layer.msg("认证成功");
				setInterval(function() {
					window.location.href = "../index/index.html";
				}, 3000);
			}
			if(data.status == 250) {
				layer.msg("请重新登录");
				setInterval(function() {
					window.location.href = "../login/log_in.html";
				}, 2000);
			}
			if(data.status == 400) {
				layer.msg("认证失败，请重新认证");
			}
		},
		error: function() {
			layer.msg('系统繁忙', function() {
				history.go(0)
			}, 1000);
		}
	});
});

/*监听手机返回按键*/
return_keybord();

})
